package com.example.mybatis.controller;

import com.example.mybatis.common.Result;
import com.example.mybatis.model.User;
import com.example.mybatis.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@EnableAutoConfiguration
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping("/getUserById/{id}")
    public User getUserById(@PathVariable Long id) throws Exception {
        return userService.getUserById(id);
    }
    @GetMapping("/getUserByList")
    public ResponseEntity<Result> getUserByList(
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @RequestParam(value = "pageSize", required = false, defaultValue = "5") int pageSize
    ) {
        Result res = new Result(200, "ok");

        List<User> result = userService.listUser(page, pageSize);
        PageInfo<User> pi = new PageInfo<>(result);

        res.putData("data", pi);
        return ResponseEntity.ok(res);
    }

}
